<!DOCTYPE html>
<html lang="en">
<head>
    {template 'common/edithead'}
    <!--微擎 -->
    <!--这个放在最后一切平白安全-->
    <script type="text/javascript" src="./resource/js/require.js?v=20170915"></script>
    <script type="text/javascript">
        //编辑器里使用
        var urobj=[];
    </script>
    <style>
        .param .input-group{display: flex;}
        .param .input-group input{flex: 1;}
        .param .input-group-addon,.param .input-group-btn{width: auto;}
    </style>
</head>
<body class="nav-md" style="min-width: 800px">
<div>
    <div class="container xc_edit_from" >
        <div class="main_container" style="overflow-x: hidden;min-height: calc(100vh - 20px);background-color: white;width: 100%;">
            <div class="col-md-12 col-xs-12">
                <div class="x_panel">
                    <ul class="we7-page-tab">
                        <li >
                            <a  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}">
                                {$xtitleb}- {$xtitlea}列表  </a></li>
                        <li><a href="{if $xc_admin['type']==1 || $xc_admin['type']==2 || ($xc_admin['type']==3 && $xc_admin['manage']['area_add']==1)}{php echo $this->createWebUrl($do, array('op'=>'edit','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}{/if}">{$xtitleb}- {$xtitlea}增加</a></li>
                    </ul>
                    <div class="x_content">
                        <br/>
                        <form class="form-horizontal form-label-left input_mask" method="post" action="{php echo $this->createWebUrl($do, array('op'=>'save'));}" id="xc_form">
                            <input type="hidden" name="id" value="{$xc['id']}">
                            <input type="hidden" name="xc[format]" value="">
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">课程</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <select  class="form-control js-select2" name="xc[pid]" data-select="{$xc['pid']}">
                                        <option value="">请选择课程</option>
                                        {loop $class $index $item}
                                        <option value="{$item['id']}" {if $item['id']==$xc['pid']}selected{/if} >{$item['name']}</option>
                                        {/loop}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">标识</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="标识" name="xc[mark]" value="{$xc[mark]}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">封面</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    {php echo tpl_form_field_image('xc[simg]',$xc['simg']);}
                                    <span class="help-block">100*100</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">轮播图</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    {php echo tpl_form_field_multi_image('xc[bimg]',$xc['bimg']);}
                                    <span class="help-block">100*100</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">原价</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="原价" name="xc[price]" value="{$xc[price]}">
                                </div>
                            </div>
                            <div class="form-group param" data-name="xc[format]">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">阶梯团</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    {if $xc['format']}
                                    {loop $xc['format'] $index $item}
                                    <div class="input-group">
                                        <input type="text" data-name="member" class="form-control" autocomplete="off" value="{$item['member']}" placeholder="人数">
                                        <input type="text" data-name="price" class="form-control" autocomplete="off" value="{$item['price']}" placeholder="价格">
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/loop}
                                    {else}
                                    <div class="input-group">
                                        <input type="text" data-name="member" class="form-control" autocomplete="off" placeholder="人数">
                                        <input type="text" data-name="price" class="form-control" autocomplete="off" placeholder="价格">
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/if}
                                </div>
                            </div>
                            <script>
                                var parameter={
                                    parameter_add:function(objc){
                                        $(objc).parent().after('<div class="input-group">' +
                                                '<input type="text" data-name="member" class="form-control" autocomplete="off" placeholder="人数">' +
                                                '<input type="text" data-name="price" class="form-control" autocomplete="off" placeholder="价格"> ' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_add(this)"> ' +
                                                '<button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button> ' +
                                                '</span> ' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_del(this)"> ' +
                                                '<button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button> ' +
                                                '</span> ' +
                                                '</div>');
                                        group();
                                    },
                                    parameter_del:function(objc){
                                        if($(objc).parent().siblings().length>0){
                                            $(objc).parent().remove();
                                        }
                                    }
                                }
                            </script>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">团购时间</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="团购时间" name="xc[group_times]" value="{$xc[group_times]}">
                                    <span class="help-block">单位：小时</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">已团</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="已团" name="xc[sold]" value="{$xc[sold]}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">名额</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="名额" name="xc[member]" value="{$xc[member]}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">截止时间</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    {php echo tpl_form_field_date('xc[end_time]',$xc['end_time'],true);}
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">状态</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="checkbox" class="js-switch" value="1" name="xc[status]"
                                           data-value="{$xc['status']}" data-field="status">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">排序</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    <input type="text" class="form-control" placeholder="排序" name="xc[sort]" value="{$xc[sort]}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2 col-sm-3 col-xs-12">活动说明</label>
                                <div class="col-md-10 col-sm-9 col-xs-12">
                                    {php echo tpl_ueditor('xc[content]',$xc['content']);}
                                </div>
                            </div>
                            <div class="form-group xc_btn_row"  >
                                <div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-5 col-sm-offset-5  col-md-xs-5 ">
                                    <button type="button" class="btn btn-primary" id="btsave">确认</button>
                                    <a type="button" class="btn btn-default"  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=> urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea)));}" >返回</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{template 'common/editfoot'}
<script>
    function formatRepo (repo) {
        if (repo.loading) {
            return repo.text;
        }
        return repo.text;
    }
    function formatRepoSelection (repo) {
        return repo.text || repo.id;
    }
    //异步slect2
    $(".ajax-select").each(function () {
        var $_tmeurl=$(this).attr("data-url");
        $(this).select2({
            language:'zh-CN',
            ajax: {
                url: $_tmeurl,
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        q: params.term, // search term
                        page: params.page
                    };
                },
                processResults: function (data, params) {
                    params.page = params.page || 1;
                    return {
                        results: data.rows,
                        pagination: {
                            more: (params.page * 30) < data.total
                        }
                    };
                },
                cache: true
            },
            placeholder: '请选择分类',
            escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
            minimumInputLength: 0,
            templateResult: formatRepo,
            templateSelection: formatRepoSelection
        });

    });
    $(function () {

        $(".ajax-select,.js-select2").change(function () {
            $("#xc_form").valid();
        })
    });
    //input 绑定回车操作
//    $("#xc_form").bind("keydown","input",function(e){
//        // 兼容FF和IE和Opera
//        var theEvent = e || window.event;
//        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
//        if (code == 13) {
//            //回车执行查询
//            $("#btsave").click();
//        }
//    });
    //现在只在状态这里使用
    var xvalidator =null;
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".col-xs-12").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".col-xs-12").removeClass("has-error").addClass("has-success")
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }), $().ready(function () {
        var e = "<i class='fa fa-times-circle'></i> ";
        var vlidp = {
            rules: {
                "xc[name]": {required: !0},
                "xc[bimg]":{url:false}
            },
            messages: {
                "xc[name]": {required: e + "请输入名称"}
            },
            submitHandler: function (form) {
                //手动验证所以这里不需要执行
                xajaxfrom(form);
                return false;
            }
        };
        xvalidator= $("#xc_form").validate(vlidp);
    });
    $("#btsave").click(function () {
        xajaxfrom("#xc_form");
        return false;
    });
    function message(data) {
        $mesoption = {};
        if (data["status"] === 1) {
            if ($('[name="id"]').val() == ""||$('[name="id"]').val() == "0") {
                xc_form.reset();
                //
                $(".container").find(".img-thumbnail").attr("src", "./resource/images/nopic.jpg");
                if( urobj.length>0){
                    for(var i=0;i<urobj.length;i++){
                        urobj[i].setContent("");
                    }
                }

                if(  $(".js-select2").length>0){
                    $('.js-select2').val(null).trigger("change");
                }
                if(  $(".ajax-select").length>0){
                    $('.ajax-select').val(null).trigger("change");
                }

            }
            $mesoption["timer"] = 1000;
            $mesoption["type"] = replyrdata[data["status"]];
        }
        $mesoption["title"] = data["message"];
        $mesoption["text"] = data["message"];
        swal($mesoption);
    }
    function xajaxfrom(form) {
        $actfrom = $(form);
        getJson();
        var  $postdate=$actfrom.serialize();
        xpagecss.xload();
        $.ajax({
            type: $(form).attr("method"),
            url: $(form).attr("action"),
            dataType: "json",
            data: $postdate,
            success: function (msg) {
                message(msg)
            }
        });
    }

    function getJson(){
        $(".param").each(function(){
            var list=[];
            var name=$(this).attr("data-name");
            $(this).find(".input-group").each(function(){
                var data={};
                $(this).find("input").each(function(){
                    if($(this).val()!=""){
                        data[$(this).attr("data-name")]=$(this).val();
                    }
                });
                if(JSON.stringify(data)!="{}"){
                    list.push(data);
                }
            });
            if(list.length>0){
                $("input[name='"+name+"']").val(JSON.stringify(list));
            }
        });
    }
</script>
</body>
</html>